package com.macllvm.whale.settings;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Environment;
import android.util.Log;
import java.util.HashMap;
import com.macllvm.whale.R;

public class SystemSettings {
    private static final String TAG = "SystemSettings";
    public static final String[] KEY_NAME = new String[]{"host_name", "nick_name", "sign", "download_path"};
    private static final String PREFERENCE_NAME = "whale_settings";
    private static final String FIRST = "first";
    private Context mContext;
    private HashMap<String, Object> settingValue = new HashMap<String, Object>();
    private SharedPreferences mPreferences;
    private HashMap<String, String> mTitleMap = new HashMap<String, String>();
    private String localIP;
    private static final String HOST_NAME = "whale im";
    private static final String NICK_NAME = "whale client";
    private static final String SIGNATURE = "";
    public  static final String DOWNLOAD = Environment.getExternalStorageDirectory().getAbsolutePath() + "/WhaleIM/download";

    public void setSettingValue(String key, Object value) {
        if(settingValue.containsKey(key)) {
            settingValue.remove(key);
        }
        settingValue.put(key, value);
    }

    public void setLocalIP(String ip) {
        localIP = ip;
    }

    public String getLocalIP() {
        return localIP;
    }

    public Object getSettingValue(String key) {
        return settingValue.get(key);
    }

    public SystemSettings(Context context) {
        this.mContext = context;

        mPreferences = mContext.getSharedPreferences(PREFERENCE_NAME, 0);
        mTitleMap.put(KEY_NAME[0], mContext.getString(R.string.settings_host));
        mTitleMap.put(KEY_NAME[1], mContext.getString(R.string.settings_nick_name));
        mTitleMap.put(KEY_NAME[2], mContext.getString(R.string.settings_signature));
    }

    public String getItemTitle(String key) {
        return mTitleMap.get(key);
    }

    public void loadSettings() {
        if(isFirst()) {
            saveDefaultSettings();
        }

        setSettingValue(KEY_NAME[0], mPreferences.getString(KEY_NAME[0], HOST_NAME));
        setSettingValue(KEY_NAME[1], mPreferences.getString(KEY_NAME[1], NICK_NAME));
        setSettingValue(KEY_NAME[2], mPreferences.getString(KEY_NAME[2], SIGNATURE));
        setSettingValue(KEY_NAME[3], mPreferences.getString(KEY_NAME[3], DOWNLOAD));

    }

    private boolean isFirst() {
        Boolean isFirst = false;
        isFirst = mPreferences.getBoolean(FIRST, true);

        return isFirst;
    }

    public void saveDefaultSettings() {
        mPreferences.edit().putString(KEY_NAME[0], HOST_NAME).apply();
        mPreferences.edit().putString(KEY_NAME[1], NICK_NAME).apply();
        mPreferences.edit().putString(KEY_NAME[2], SIGNATURE).apply();
        mPreferences.edit().putString(KEY_NAME[3], DOWNLOAD).apply();
        mPreferences.edit().putBoolean(FIRST, false).apply();
    }

    public void saveSettings(String key, Object value) {
        if(value instanceof String){
            String val = (String)value;
            mPreferences.edit().putString(key, val).apply();
        }else if(value instanceof Integer){
            Integer val = (Integer)value;
            mPreferences.edit().putInt(key, val).apply();
        }else if(value instanceof Long){
            Long val = (Long)value;
            mPreferences.edit().putLong(key, val).apply();
        }else if(value instanceof Float){
            Float val = (Float)value;
            mPreferences.edit().putFloat(key, val).apply();
        }else if(value instanceof Boolean){
            Boolean val = (Boolean)value;
            mPreferences.edit().putBoolean(key, val).apply();
        }
    }


}